package com.njupt.wuaiagent.rag;

import com.njupt.wuaiagent.app.LoveApp;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.stereotype.Component;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/5/17 21:58
 * @Description: 创建自定义RAG检索增强顾问工厂
 * @Version: 1.0
 */



public class LoveAppRagCustomAdvisorFactory {


    /**
     * 创建自定义的RAG检索增强顾问
     * @param vectorStore  向量存储
     * @param status       过滤状态（标间）
     * @return 返回自定义 RAG 检索增强顾问
     */
    public static Advisor creteLoveAppRagCustomAdvisor(VectorStore vectorStore,String status){

        Filter.Expression expression = new FilterExpressionBuilder()
                .eq("status", status)
                .build();

        //文档的检索底层还是用到向量数据库的相似度搜索的的searchRequest
        DocumentRetriever documentRetriever = VectorStoreDocumentRetriever.builder()
                .vectorStore(vectorStore)                   //指定一个向量数据库
                .similarityThreshold(0.5)                   //指定相似度阈值
                .filterExpression(expression)               //过滤条件
                .topK(3)                                    //返回的文档数
                .build();

        return RetrievalAugmentationAdvisor.builder()
                .documentRetriever(documentRetriever)    //文档的检索和过滤

                //文档的增强和关联
                .queryAugmenter(LoveAppContextualAugmenterFactory.createInstance())
                .build();
    }
}
